<template>
	<view>
		<view class="top" @tap="upload">
			上传图片
		</view>
		<image :src="img"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 'pearGarden',
				img: ''
			}
		},
		methods: {
			upload() {
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera '], //从相册或相机选择
					success: (res) => {
						console.log(JSON.stringify(res.tempFilePaths));
						const tempFilePaths = res.tempFilePaths;
						for (var i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: this.url + '/files/upload',
								filePath: tempFilePaths[i],
								name: 'file',
								formData: {
									user: '哈哈',
									url: 'user',
									name: this.id + (new Date().getTime() + i).toString()
								},
								success: (res) => {
									this.img=(this.url + '/user/' + (JSON.parse(res.data).filename).toString()).replace(/\\/g, "/")
									console.log(JSON.parse(res.data));
									console.log(this.img)
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
	.top {
		margin: 50px 0;
		text-align: center;
	}
</style>
